﻿$G.web.ajax = {
	ajaxBusy: false,

	xhrErrorCode: -65535,

	toJSONData: function (data) {
		var d = { token: JSON.stringify(data) };
		return JSON.stringify(d);
	},

	initialize: function () {
		$(document).bind("ajaxStart", $G.web.ajax.ajaxindicatorstart).bind("ajaxStop", $G.web.ajax.ajaxindicatorstop);
	},

	call: function (method, data, s, e) {

		$.ajax({
			type: "POST",
			url: "/Ajax/WebMethods.asmx/" + method,
			data: this.toJSONData(data),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			async: true,
			success: function (msg) { $G.web.ajax.ajaxSuccess(msg, s, e); },
			error: function (xhr, status, error) { $G.web.ajax.ajaxError(xhr, status, error, e); },
		});
	},

	ajaxSuccess: function (msg, s, e) {
		if (msg.d.ErrorCode != '0') {
			if (typeof e !== "undefined") e({ success: false, errorCode: msg.d.ErrorCode, errorMessage: msg.d.ErrorMessage });
		} else if (typeof s !== "undefined") s(msg);
	},

	ajaxError: function (xhr, status, error, e) {
		if (typeof e !== undefined) {
			var err;
			try {
				err = JSON.parse(xhr.responseText);
			}
			catch (exc) {
				err = { Message: xhr.responseText };
			}
			e({ success: false, errorCode: this.xhrErrorCode, errorMessage: err.Message });
		}
	},

	ajaxindicatorstart: function()
	{
		$G.web.ajax.ajaxBusy = true;

		if ($('body').find('#resultLoading').attr('id') != 'resultLoading') {
			$('body').append('<div id="resultLoading" style="display:none"><div><img src="/Cashier/ajax-loader.gif"><div>Bezig...</div></div><div class="bg"></div></div>');
		}

		$('#resultLoading').css({
			'width':'100%',
			'height':'100%',
			'position':'fixed',
			'z-index':'10000000',
			'top':'0',
			'left':'0',
			'right':'0',
			'bottom':'0',
			'margin':'auto'
		});

		$('#resultLoading .bg').css({
			'background':'#000000',
			'opacity':'0.7',
			'width':'100%',
			'height':'100%',
			'position':'absolute',
			'top':'0'
		});

		$('#resultLoading>div:first').css({
			'width': '250px',
			'height':'75px',
			'text-align': 'center',
			'position': 'fixed',
			'top':'0',
			'left':'0',
			'right':'0',
			'bottom':'0',
			'margin':'auto',
			'font-size':'16px',
			'z-index':'10',
			'color':'#ffffff'

		});

		$('#resultLoading .bg').height('100%');
		$('body').css('cursor', 'wait');

		window.setTimeout(function () {
			if ($G.web.ajax.ajaxBusy) {
				$('#resultLoading').fadeIn(300);
			}
		}, 500);
	},

	ajaxindicatorstop: function()
	{
		$G.web.ajax.ajaxBusy = false;
		$('#resultLoading .bg').height('100%');
		$('#resultLoading').fadeOut(300);
		$('body').css('cursor', 'default');
	},

	userLogin: function (data, successCallback, errorCallback) { $G.web.ajax.call("GetWebUserLogin", data, successCallback, errorCallback); },
	resetWW: function (data, successCallback, errorCallback) { $G.web.ajax.call("ResetPW", data, successCallback, errorCallback); },
	registerConsumer: function (data, successCallback, errorCallback) { $G.web.ajax.call("RegisterConsumer", data, successCallback, errorCallback); },
	updateUser: function (data, successCallback, errorCallback) { $G.web.ajax.call("UpdateUser", data, successCallback, errorCallback); },
	sendCode: function (data, successCallback, errorCallback) { $G.web.ajax.call("SendGollieCode", data, successCallback, errorCallback); },
	registerVendorUser: function (data, successCallback, errorCallback) { $G.web.ajax.call("RegisterVendorUser", data, successCallback, errorCallback); },
	resetVendorUserWW: function (data, successCallback, errorCallback) { $G.web.ajax.call("NewPassword", data, successCallback, errorCallback); },
	initKvK: function (data, successCallback, errorCallback) { $G.web.ajax.call("InitKvK", data, successCallback, errorCallback); },
	registervendor: function (data, successCallback, errorCallback) { $G.web.ajax.call("RegisterVendor", data, successCallback, errorCallback); },
	getUserProfile: function (data, successCallback, errorCallback) { $G.web.ajax.call("GetUserProfile", data, successCallback, errorCallback); },
	getVendorSettings: function (data, successCallback, errorCallback) { $G.web.ajax.call("GetVendorSettings", data, successCallback, errorCallback); },
	updateVendorSettings: function (data, successCallback, errorCallback) { $G.web.ajax.call("UpdateVendorSettings", data, successCallback, errorCallback); },
}

$G.web.ajax.initialize();